package com.lngsyz.ywtb.biz.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lngsyz.ywtb.entity.biz.ReferPhySeal;
import com.lngsyz.ywtb.entity.source1.SystemSeal;
import com.lngsyz.ywtb.enumeration.SealMedia;
import com.lngsyz.ywtb.enumeration.SealStatus;
import com.lngsyz.ywtb.enumeration.SealType;
import com.lngsyz.ywtb.mapper.source1.SystemSealMapper;

import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;

@Service
public class SystemSealService {
    
	@Autowired
	SystemSealMapper systemSealMapper;
	
    public List<SystemSeal> findByCustomIdAndType(String customId, String type){
    	Map<String, Object> param = new HashMap<String, Object>();
    	if(StringUtils.isNotBlank(customId)) {
    		param.put("customid", customId);
    		param.put("type", type);
    		List<SystemSeal> lists = systemSealMapper.findByCondition(param);
    		return lists;
    	} else {
    		return new ArrayList<SystemSeal>(); 
    	}
    }
    
    public SystemSeal findById(String id) {
    	if(StringUtils.isNotBlank(id)) {
    		return systemSealMapper.findById(id);
    	} else {
    		return null;
    	}
    } 
    
    public List<ReferPhySeal> findReferSeal(String customId){
    	Map<String, Object> param = new HashMap<String, Object>();
    	if(StringUtils.isNotBlank(customId)) {
    		param.put("customid", customId);
    		param.put("status", SealStatus.DELIVERY.getKey());
    		param.put("mediatype", SealMedia.PHYSICAL.getKey());
    		List<SystemSeal> lists = systemSealMapper.findByCondition(param);
    		List<ReferPhySeal> rtn = new ArrayList<ReferPhySeal>();
    		for(SystemSeal ss : lists) {
    			ReferPhySeal referPhySeal = new ReferPhySeal();
    			referPhySeal.setId(ss.getId());
    			referPhySeal.setCode(ss.getCode());
    			referPhySeal.setName(ss.getName());
    			referPhySeal.setType(ss.getType());
    			referPhySeal.setTypeName(SealType.getNameByKey(ss.getType()));
    			rtn.add(referPhySeal);
    		}
    		return rtn;
    	} else {
    		return new ArrayList<ReferPhySeal>(); 
    	}
    }
    
    public ReferPhySeal getReferSealByReferId(String referSealId) {
    	SystemSeal ss = systemSealMapper.findById(referSealId);
    	ReferPhySeal referPhySeal = new ReferPhySeal();
		referPhySeal.setId(ss.getId());
		referPhySeal.setCode(ss.getCode());
		referPhySeal.setName(ss.getName());
		referPhySeal.setTypeName(SealType.getNameByKey(ss.getType()));
		return referPhySeal;
    }
}
